var XGOON = {
    initValidate: function() {
        if (jQuery('form.validate').length > 0) {
            var forms = jQuery('form.validate');
            forms.each(function() {
                var form = jQuery(this);
                var inputEmails = form.find('input.validateEmail');
                var types = ['Number', 'Email', 'Url'];
                var inputElements = form.find('input.required');
                var content = form.find('textarea.required');
                var selectbox = form.find('select.required');
                var radiobox = form.find('input.radrequired');

                form.find('.btn-submit').click(function() {
                    var error = 0;
                    if (inputElements.length > 0) {
                        inputElements.each(function() {
                            var inputElement = jQuery(this);
                            if (!XGOON.validateElement(inputElement, true)) {
                                error++;
                            }
                        });
                    }
                    if (content.length) {

                        content.each(function() {
                            if (!XGOON.isNotOnlySpace(jQuery(this).val())) {
                                error++;
                                jQuery(this).addClass('error');
                                jQuery(this).parent().append('<label class="error">This field is required!</label>');
                            }
                        });
                    }
                    if (radiobox.length) {
                        var groupName = new Array();
                        radiobox.each(function() {
                            var name = jQuery(this).attr('name');
                            if (jQuery.inArray(name, groupName) < 0) {
                                groupName.push(name);
                            }
                        });
                        for (var i = 0; i < groupName.length; i++) {
                            var radBox = jQuery('input[name="' + groupName[i] + '"]');
                            if (!XGOON.validateRadiobox(groupName[i])) {
                                error++;
                                radBox.parents('.wrapper-input').find('label.error').remove();
                                radBox.parents('.wrapper-input').append('<label class="error">This field is required!</label>');
                            }
                            else {
                                radBox.parents('.wrapper-input').remove('label.error');
                            }
                        }
                    }
                    if (selectbox.length) {

                        selectbox.each(function() {
                            if (!XGOON.validateSelectbox(jQuery(this))) {
                                error++;
                            } else {
                                jQuery(this).removeClass('error');
                                jQuery(this).parent().remove('label.error');
                            }
                        });
                    }
                    for (var i = 0; i < types.length; i++) {
                        var inputTypes = form.find('input.validate' + types[i]);
                        if (inputTypes.length > 0) {
                            inputTypes.each(function() {
                                var inputType = jQuery(this);
                                if (jQuery.trim(inputType.val()) !== '') {
                                    if (!XGOON.validateElement(inputType, false, types[i])) {
                                        error++;
                                    }
                                }
                            });
                        }
                    }
                    if (error > 0) {
                        return false;
                    }
                });
                for (var i = 0; i < types.length; i++) {
                    var inputTypes = form.find('input.validate' + types[i]);
                    if (inputTypes.length > 0) {
                        inputTypes.each(function() {
                            var inputType = jQuery(this);
                            var error = 0;
                            inputType.keyup(function() {
                                if (inputType.val().length > 0) {
                                    if (!XGOON.validateElement(inputType, false, types[i])) {
                                        error++;
                                    }
                                }
                            });
                            if (error > 0) {
                                return false;
                            }

                        });
                    }
                }
                if (selectbox.length) {
                    var error = 0;
                    selectbox.change(function() {
                        if (!XGOON.validateSelectbox(jQuery(this))) {
                            error++;
                        } else {
                            error = 0;
                            jQuery(this).removeClass('error');
                            jQuery(this).parent().find('label.error').remove();
                        }
                    });
                    if (error > 0) {
                        return false;
                    }
                }
                if (radiobox.length) {
                    var error = 0;
                    radiobox.change(function() {

                        if (!XGOON.validateRadiobox(jQuery(this).attr('name'))) {
                            error++;
                            jQuery(this).parents('.wrapper-input').find('label.error').remove();
                            jQuery(this).parents('.wrapper-input').append('<label class="error">This field is required!</label>');
                        }
                        else {
                            jQuery(this).parents('.wrapper-input').find('label.error').remove();
                        }
                    });
                }
                inputElements.each(function() {
                    var inputElement = jQuery(this);
                    var error = 0;
                    inputElement.keyup(function() {
                        if (!XGOON.validateElement(inputElement, true)) {
                            error++;
                        }
                    });
                    if (error > 0) {
                        return false;
                    }
                });
            });
        }
    },
    isNotOnlySpace: function(sString) {
        while (sString.substring(0, 1) == ' ')
        {
            sString = sString.substring(1, sString.length);
        }
        while (sString.substring(sString.length - 1, sString.length) == ' ')
        {
            sString = sString.substring(0, sString.length - 1);
        }
        if (sString === '') {
            return false;
        }
        else {
            return true;
        }
    },
    validateSelectbox: function(selectBoxElement) {
        selectBoxElement.parent().find('label.error').remove();
        if ((jQuery.trim(selectBoxElement.val()) == '') || (parseInt(jQuery.trim(selectBoxElement.val())) == 0)) {
            selectBoxElement.addClass('error');
            selectBoxElement.parent().append('<label class="error">This field is required!</label>');
            return false;
        }
        return true;
    },
    validateRadiobox: function(radioBoxName) {
        var radioBox = jQuery('input[name="' + radioBoxName + '"]');
        var check = 0;
        radioBox.each(function() {
            if (jQuery(this).is(':checked')) {
                check = 1;
            }
        });
        return check;
    },
    isEmail: function(val) {
        var regEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
        return regEmail.test(val);
    },
    isUrl: function(val) {
        var regUrl = /(http|https|ftp|mailto):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
        return regUrl.test(val);
    },
    initScrollToTopPage: function() {
        if (jQuery('.scrolltoTop').length) {
            jQuery('.scrolltoTop').click(function() {
                jQuery('html, body').animate({scrollTop: 0}, 800);
                return false;
            });
        }

    },
    validateElement: function(inputElement, required, type, minlength) {
        if (!type) {
            type = '';
        }
        inputElement.parent().find('label.error').remove();
        inputElement.removeClass('error');
        if (required) {
            if (jQuery.trim(inputElement.val()) === '') {
                inputElement.parent().append('<label class="error">This field is require</label>');
                inputElement.addClass('error');
                return false;
            }
            if (minlength && minlength > 0) {
                if ((parseInt(minlength) > 0) && (inputElement.val().length > parseInt(minlength))) {
                    inputElement.parent().append('<label class="error">This field at least ' + parseInt(minlength) + ' characters</label>');
                    inputElement.addClass('error');
                    return false;
                }
            }
        }

        if (inputElement.hasClass('validateEmail') || inputElement.attr('type').toLowerCase() === 'email' || type.toLowerCase() === 'email') {
            if (!XGOON.isEmail(inputElement.val())) {
                inputElement.parent().append('<label class="error">This field is email</label>');
                inputElement.addClass('error');
                return false;
            }
        }
        if (inputElement.hasClass('validateUrl') || inputElement.attr('type').toLowerCase() === 'url' || type.toLowerCase() === 'url') {
            if (!XGOON.isUrl(inputElement.val())) {
                inputElement.parent().append('<label class="error">This field is url</label>');
                inputElement.addClass('error');
                return false;
            }
        }

        if ((inputElement.hasClass('validateNumber')) || (inputElement.attr('type') === 'number') || (type.toLowerCase() === 'number')) {
            if (/\D/g.test(inputElement.val()))
            {
                inputElement.val(inputElement.val().replace(/\D/g, ''));
            }
        }
        return true;

    },
    initStyleDisplay: function() {
       var header = jQuery('header');
       if(header.find('#imgLogo').length){
           XGOON.initScrollFixToolbar(header,600,jQuery('#imgLogo'));
       }else{
           XGOON.initScrollFixToolbar(header,600);
       }
    },
    initScrollFixToolbar: function(element, heightstart,fixlogo) {
        if (element.length) {
            jQuery(window).bind('scroll', function() {
                var navHeight = jQuery(window).height() - heightstart;                
                if (jQuery(window).scrollTop() > navHeight) {
                    element.addClass('navfixed');
                }
                else {
                    element.removeClass('navfixed');
                }
            });
        }
    },
    initJcarouselSlide: function() {
        if (jQuery('.jcarousel').length > 0) {
            if (jQuery('.lstproductsInner li').length < 4) {
                jQuery('.jcarousel-control').css({'display': 'none'});
            } else {
                jQuery('.jcarousel').jcarousel();
                jQuery('.jcarousel-control-prev').on('jcarouselcontrol:active', function() {
                    jQuery(this).removeClass('inactive');
                }).on('jcarouselcontrol:inactive', function() {
                    jQuery(this).addClass('inactive');
                }).jcarouselControl({
                    target: '-=1'
                });

                jQuery('.jcarousel-control-next').on('jcarouselcontrol:active', function() {
                    jQuery(this).removeClass('inactive');
                }).on('jcarouselcontrol:inactive', function() {
                    jQuery(this).addClass('inactive');
                }).jcarouselControl({
                    target: '+=1'
                });
            }
        }
    },
    initTabs: function() {
        if (jQuery('.tabsContent').length) {
            jQuery('.tabsContent li').each(function() {
                var id = jQuery(this).data('tab');
                if (jQuery(this).hasClass('active')) {
                    jQuery('#' + id).css({'display': 'inline-block'});
                }
                jQuery(this).click(function() {
                    jQuery('.tabsContent li').removeClass('active');
                    jQuery(this).addClass('active');
                    jQuery('.tabsContentMix').css({'display': 'none'});
                    jQuery('#' + id).css({'display': 'inline-block'});
                });
            });
        }
    },
    initDatepicker: function() {
        if (jQuery("input.datepicker").length) {
            jQuery("input.datepicker").datepicker({
                changeMonth: true,
                changeYear: true
            });
            jQuery("input.datepicker").datepicker("option", "showAnim", 'slideDown');
        }

    },
    initMixitUp: function() {
        if (jQuery('#navMenu').length) {
            jQuery('#navMenu').mixitup({
                targetSelector: '.mix',
                filterSelector: '.filter',
                sortSelector: '.sort',
                buttonEvent: 'click',
                effects: ['fade', 'scale', 'blur', 'grayscale'], //  'rotateZ', 'rotateX','rotateY',
                listEffects: null,
                easing: 'smooth',
                layoutMode: 'grid',
                targetDisplayGrid: 'inline-block',
                targetDisplayList: 'block',
                gridClass: '',
                listClass: '',
                transitionSpeed: 600,
                showOnLoad: 'all',
                sortOnLoad: false,
                multiFilter: false,
                filterLogic: 'or',
                resizeContainer: true,
                minHeight: 0,
                failClass: 'fail',
                perspectiveDistance: '3000',
                perspectiveOrigin: '50% 50%',
                animateGridList: true,
                onMixLoad: null,
                onMixStart: null,
                onMixEnd: null
            });
        }
    },
    initpopupMain: function() {
        if (jQuery('.callpopup').length) {
            jQuery('.callpopup').click(function() {
                var winH = jQuery(document).height();
                var winW = jQuery(window).width();
                jQuery('#mask').css({
                    'width': winW,
                    'height': winH
                });
                var popupclass = jQuery(this).data('popup');
                var popup = jQuery('.' + popupclass);
                jQuery('#mask').fadeIn(500);
                jQuery('#mask').fadeTo("slow", 0.5);
                popup.fadeIn(500);
                var left = (winW / 2 - popup.width() / 2);
                popup.css({'top': jQuery(window).height() / 5, 'left': left});
                jQuery(window).scroll(function() {
                    var st = jQuery(window).scrollTop();
                    popup.css({'top': st + jQuery(window).height() / 5});
                });
                jQuery(window).resize(function() {
                    var winW = jQuery(window).width();
                    jQuery('#mask').css({'width': winW, 'height': winH});
                    var sl = (winW / 2 - popup.width() / 2);
                    popup.css({'left': sl});
                });
            });
            var KEYCODE_ESC = 27;
            jQuery('#mask,.icon-close-pop').click(function() {
                jQuery('.popupforAll').fadeOut(500);
                jQuery('#mask').fadeOut(500);
            });
            jQuery(document).keyup(function(e) {
                if (e.keyCode == KEYCODE_ESC) {
                    jQuery('.popupforAll').fadeOut(500);
                    jQuery('#mask').fadeOut(500);
                }
            });
        }
    },
    initshowOnMobile: function() {
        if (jQuery('a.callnavMobile').length > 0) {
            jQuery('a.callnavMobile').click(function() {

                if (jQuery('nav.mainmenuMobile').hasClass('uncalled')) {
                    jQuery('nav.mainmenuMobile').animate({
                        left: "+=200"
                    });
                    jQuery('body > header,#content,footer').animate({
                        'margin-left': "+=200",
                        'margin-right': "-=200"
                    });

                } else {
                    jQuery('nav.mainmenuMobile').animate({
                        left: "-=200"
                    });
                    jQuery('body > header,#content,footer').animate({
                        'margin-left': "-=200",
                        'margin-right': "+=200"
                    });
                }
                jQuery('nav.mainmenuMobile').toggleClass('uncalled');
            });
        }
    },
    initCycleSlideshow: function() {
        if (jQuery('.cycleSlideshow').length) {
            var cycleSlideshow = jQuery('.cycleSlideshow');
            cycleSlideshow.each(function() {
                jQuery(this).before('<a class="prev cyclecontrol" href="javascript:;">‹</a><a class="next cyclecontrol" href="javascript:;">›</a><div class="cyclenav"><div class="nav" >').cycle({
                    fx: 'scrollLeft',
                    speed: 1000,
                    delay: -2000,
                    pager: jQuery(this).parent().find('.nav'),
                    next: jQuery(this).parent().find('.next'),
                    prev: jQuery(this).parent().find('.prev')
                });
            });
        }
    },
    initCheckAll: function() {
        if (jQuery('input.chk-all').length) {
            jQuery('input.chk-all').change(function() {
                var checkboxes = jQuery('input.' + jQuery(this).data('check'));
                if (jQuery(this).is(':checked')) {
                    checkboxes.prop('checked', true);
                } else {
                    checkboxes.prop('checked', false);
                }
            });
        }
    }
};
jQuery(document).ready(function() {
    XGOON.initStyleDisplay();
    XGOON.initpopupMain();
    XGOON.initValidate();
    XGOON.initScrollToTopPage();
    XGOON.initshowOnMobile();
    XGOON.initCycleSlideshow();
    XGOON.initDatepicker();
    XGOON.initCheckAll();
    XGOON.initTabs();
    XGOON.initJcarouselSlide();
});